package org.wangchaocheng.cloudgallery.user.executor.command;

import lombok.RequiredArgsConstructor;
import org.wangchaocheng.cloudgallery.common.annotation.CommandExecutor;
import org.wangchaocheng.cloudgallery.common.exception.ErrorEnum;
import org.wangchaocheng.cloudgallery.common.exception.ExceptionFactory;
import org.wangchaocheng.cloudgallery.commond.user.UserModifyCmd;
import org.wangchaocheng.cloudgallery.dto.user.UserDTO;
import org.wangchaocheng.cloudgallery.user.assembler.UserAssembler;
import org.wangchaocheng.cloudgallery.user.gateway.UserGateway;

/**
 * 用户信息修改命令执行器
 * @author wangchaocheng
 * @date 2025-03-14
 **/
@CommandExecutor
@RequiredArgsConstructor
public class UserModifyCmdExe {

    private final UserGateway userGateway;

    public void execute(UserModifyCmd cmd) {
        // 1. 校验用户是否存在
        Boolean exist = userGateway.existByColumn(UserDTO::getId, cmd.getId());
        if(Boolean.FALSE.equals(exist)){
            throw ExceptionFactory.bizException(ErrorEnum.E_BIZ_NOT_EXIST, "修改失败，当前用户不存在");
        }
        // 2. 组装用户信息
        UserDTO userDTO = UserAssembler.INSTANCE.assembleDTO(cmd);
        // 3. 保存用户信息
        userGateway.modify(userDTO);
    }
}
